package util.bean;

import org.xml.sax.SAXException;
import util.xml.XMLUtil;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

/**
 * 基于类的全限定名和反射的实例创建工具<br>
 * on 2022/12/26 1:45
 */
public class BeanUtil {
    /**
     * 根据配置文件中指定标签的值【类全限定名】，生成其实例<br>
     * @param c Class对象
     * @param configFile XML配置文件对象
     * @param tagName 配置文件中，标签的名称
     * @param <T> bean的Class类型
     * @return T类型的实例
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static <T> T newBean(Class<T> c, File configFile, String tagName)
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, ParserConfigurationException, IOException, SAXException {
        if (c == null) {
            throw new IllegalArgumentException("须指定Class对象");
        }
        c = (Class<T>) Class.forName(XMLUtil.getValueOfFirstTagOfConfigFile(configFile, tagName));
        return c.newInstance();
    }
}
